package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BUSY_FARMING_ONE_KEY_QUICKEN_GROWS,desc = "一键")
public class BusyFarmingOneKeyQuickenCmd extends AbstractCommand {

    int goodsId;

    int landId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.BusyFarmingQuickenReqMsg reqMsg = BusyFarmingProto.BusyFarmingQuickenReqMsg.parseFrom(packet.getBytes());
        landId = reqMsg.getLandId();
        goodsId = reqMsg.getGoodsId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BusyFarmingMgr.activityInTime()){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_IN_TIME;
            return;
        }
        if(landId < 0 || landId > BusyFarmingMgr.ALL_LAND_NUM - 1){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        if(BusyFarmingMgr.isRestTime()) {
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NOT_IN_OPT_TIME;
            return;
        }
        //判定肥料道具是否有效
        if(BusyFarmingMgr.BUSY_FARMING_MANURE_ID != goodsId){
            ret = GameErrorCode.E_COMMON_ACTIVITY_NO_ACTIVITY_GOODS;
            return;
        }

        ret = player.getModule(BusyFarmingModule.class).oneKeyManure(landId, goodsId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BusyFarmingProto.BusyFarmingQuickenRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingQuickenRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_BUSY_FARMING_ONE_KEY_QUICKEN_GROWS, respMsg);
        }
    }
}
